import { Component, Injector, OnInit, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { QualityPlatformServiceProxy ,CommonServiceProxy} from '@shared/service-proxies/service-proxies';
import { TreeNode } from 'primeng/api';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { Paginator } from 'primeng/components/paginator/paginator';

@Component({
    selector: 'app-multi-media',
    templateUrl: './multi-media.component.html',
    styleUrls: ['./multi-media.component.less']
})
export class MultiMediaComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;

    //型号下拉选，及选中值
    Models = []
    modelValue = undefined

    //发次下拉选及选中值
    LotNoList = []
    lotNo = undefined
    dblotNo = undefined  //对比发次
    selectedFiles: TreeNode
    //树形结构值
    treeData = []
    //树节点选中值
    selectedTree = undefined
    //树加载动画
    loadingtree: boolean = false
    ischeckTree: any;

    //项目下拉选及选中值
    projectList = []
    project = undefined

    //部位下拉选及选中值
    locationList = []
    location = undefined

    //日期范围值
    rangeDates = undefined
    images: any[];
    //日期显示中文
    chinese = {
        /** 每周第一天，0代表周日 */
        firstDayOfWeek: 0,
        /** 每周天数正常样式 */
        dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        /** 每周天数短样式（位置较小时显示） */
        dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        /** 每周天数最小样式 */
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        /** 每月月份正常样式 */
        monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        /** 每月月份短样式 */
        monthNamesShort: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
    }

    workStepData: any;//工步动态表List
    workStepHeader: any;//工步动态表Header

    constructor(injector: Injector,
        private _qualityPlatformServiceProxy: QualityPlatformServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
        private _messageService: MessageService) {
        super(injector);
    }
    getUser(item) {
        let msg = item ? item.split('，') : [];
        // let result = [];
        // for (const i of msg) {
        //     let data = i.split('_');
        //     result.push(data);
        // }
        let rs = [];
        for (const item of msg) {
            let msg = item.split('*');
            let data = { name: undefined, time: undefined, url: undefined };
            data.name = msg[0];
            data.time = msg[1];
            data.url = msg[2];
            rs.push(data);
        }
        return rs;
    }
    //项目查询
    getProjectList() {
        this.projectList = []
        this._qualityPlatformServiceProxy.getRecordElementList(this.project)
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(item => {
                    this.projectList.push({
                        label: item,
                        value: item
                    })
                })
            })
    }
    //部位查询
    getLocationList() {
        this.locationList = []
        this._qualityPlatformServiceProxy.getRecordLocationList(this.location)
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(item => {
                    this.locationList.push({
                        label: item,
                        value: item
                    })
                })
            })
    }

    //查询树形结构
    treeSearch() {
        if (!Boolean(this.modelValue && this.lotNo)) {
            this._messageService.warn('请先选择型号和发次!')
            return
        }
        this._qualityPlatformServiceProxy.getTreeContainRoot(this.lotNo, this.modelValue)
            .pipe(finalize(() => { })).subscribe(result => {
                this.treeData = [];
                this.treeData.push({
                    ...result.parentNode,
                    key: -1,
                    nodeDrawingNo: result.parentNode.drawingNo,
                    nodeName: result.parentNode.modelName,
                    model: result.parentNode.model,
                    lotNo: result.parentNode.lotNo,
                    stageSign: result.parentNode.stageSign,
                    children: result.childNodes
                })
                this.expandAll();
            })
    }
    //全部展开
    expandAll() {
        this.treeData.forEach((node) => {
            this.expandRecursive(node, true);
        });
    }
    private expandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.expandRecursive(childNode, isExpand);
            });
        }
    }
    //树节点选中操作
    nodeSelect(event) {
        this.ischeckTree = event.node;
    }
    //根据表名搜索
    searchRight() {
        // if (!Boolean(this.modelValue && this.lotNo && this.dblotNo && this.dblotNo.length > 0)) {
        //     this._messageService.warn('请先选择型号和发次和对比发次!')
        //     return
        // }
        if (!Boolean(this.ischeckTree)) {
            this._messageService.warn('请先选择树节点!')
            return
        }
        //日期参数可以为空
        let start: any = undefined
        let end: any = undefined
        if (this.rangeDates) {
            start = this.dateFormat(this.rangeDates[0])
            end = this.dateFormat(this.rangeDates[1])
        }
        this._qualityPlatformServiceProxy.multiMediaReport(this.modelValue,
            this.lotNo,
            this.ischeckTree ? this.ischeckTree.id : undefined, this.project,
            this.location, start, end, this.ischeckTree.key != -1 ? false : true,
            this.ischeckTree ? this.ischeckTree.nodeLevel : undefined,
            this.paginator ? this.paginator.getPage() + 1 : 1, this.paginator ? this.paginator._rows : 5,this.dblotNo)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                if (result.multiMediaExport && result.multiMediaExport.length > 0) {
                    this.workStepHeader = result.tableNameDictionary;
                    // this.workStepData = result.tableNameDictionary[result.tableNameDictionary.tableKey];
                    this.workStepData = result.multiMediaExport;
                } else {
                    this._messageService.info('暂无数据！')
                }
            });
    }
    getImage(item) {
        let imageList = item ? item.split(',') : []
        let result = []
        imageList.forEach((e, i) => {
            if (e.includes('.mp4')) {
            } else {
                let a = e.split('_');
                a[a.length - 1]=a[a.length - 1].replace('thumbnail', '');
                let rs = a.join('');
                result.push({
                    source: rs,
                    thumbnail: rs,
                    title: ''
                })
            }
        })
        return result
    }
    getVideo(item) {
        let imageList = item ? item.split(',') : []
        let result = []
        imageList.forEach((e, i) => {
            if (e.includes('.mp4')) {
                result.push({
                source: e,
                thumbnail: e,
                title: ''
            })
            } 
        })
        return result
    }

    //取消，重置右侧下拉框、日期等数据
    resetSearchRight(): void {
        this.project = undefined;
        this.location = undefined;
        this.dblotNo = undefined;
    }
    //日期格式化
    dateFormat(str): string {
        let date = new Date(str)
        let year: any = date.getFullYear()
        let month: any = date.getMonth() + 1
        if (month < 10) {
            month = '0' + month
        }
        let day: any = date.getDate()
        if (day < 10) {
            day = '0' + day;
        }
        return year + '-' + month + '-' + day
    }
    ngOnInit() {
          //获取型号下拉
            this.Models = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            this._qualityPlatformServiceProxy.getModelNameList(undefined)
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    for (const item of result) {
                        let data = { label: undefined, value: undefined };
                        data.label = item;
                        data.value = item;
                        this.Models.push(data);
                    }
                });
        //查询项目
        this.getProjectList();
        //查询部位
        this.getLocationList();
    }
    onChange(event) {
        //获取发次
        this.LotNoList = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._qualityPlatformServiceProxy.getLotNoList(undefined,this.modelValue)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.LotNoList.push(data);
                }
            });
    }

}
